<template>
  <div class="reported">
    <!--  :inline="true"-->
    <el-form
      ref="reportForm"
      :model="reportForm"
      label-width="110px"
      class="form-left"
      :rules="reportRule"
    >
      <el-row>
        <el-col :span="8">
          <el-form-item label="文件类型：" prop="fileType">
            <el-select v-model="reportForm.fileType" placeholder="请选择" clearable filterable>
              <el-option
                v-for="item in fileTypeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="文件名：" prop="fileName">
            <el-input  placeholder="请输入文件名" v-model="reportForm.fileName" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="是否共享：" prop="fileStatus">
            <el-select v-model="reportForm.fileStatus" placeholder="请选择是否共享" clearable filterable>
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
            </el-select>
            </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="研究主题：" prop="fileTheme">
            <el-input   placeholder="请输入研究主题" v-model="reportForm.fileTheme" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="访问密码：" prop="filePassword">
            <el-input  placeholder="请选择输入访问密码" v-model="reportForm.filePassword" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="文件大小：" prop="fileSize">
            <el-input  :disabled="true" placeholder="上传文件后自动生成文件大小" v-model="reportForm.fileSize" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="上传文件：" prop="filePath">
            <el-upload
              class="upload-demo"
              drag
              show-file-list="false"
              limit="1"
              :action="uploadUrl"
              :before-upload="beforeUploadHandle"
              :on-success="successHandle"
              ref="upload"
            >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div class="el-upload__tip" slot="tip">注意：单个文件请不要上传超过500MB</div>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="5">
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="submitForm()">上传</el-button>
        <el-button @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        fileTypeList: [{
          id: 0,
          name: '数据文件'
        }, {
          id: 1,
          name: '图像文件'
        }, {
          id: 2,
          name: '视频文件'
        }, {
          id: 3,
          name: '论文文件'
        }, {
          id: 4,
          name: '各类电子文书'
        }],
        options: [{
          value: 0,
          label: '共享'
        }, {
          value: 1,
          label: '私有'
        }],
        uploadUrl: '',
        reportForm: {
          id: '',
          fileType: '',
          fileName: '',
          fileStatus: '',
          fileTheme: '',
          fileSize: '',
          filePassword: ''
        },
        reportRule: {
          fileType: [
            { required: true, message: '文件类型不能为空', trigger: ['blur','change'] }
          ],
          fileName: [
            { required: true, message: '文件名不能为空', trigger: 'blur' }
          ],
          fileStatus: [
            { required: true, message: '是否共享不能为空', trigger: ['blur','change'] }
          ],
          fileTheme: [
            { required: true, message: '研究主题不能为空', trigger: 'blur' }
          ],
          fileSize: [
            { required: true, message: '请上传文件', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.uploadUrl = this.$http.adornUrl(`/oss/upload?token=${this.$cookie.get('token')}`);
      let files=this.$route.query['row']
      if(files){
        this.reportForm = files
      }
    },
    methods: {
      resetForm() {
        this.$refs['reportForm'].resetFields()
        // 清空上传附件
        this.$refs.upload.clearFiles()
      },
      submitForm() {
        this.$refs['reportForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/fileManage/${!this.reportForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData(this.reportForm)
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '上传成功',
                  type: 'success'
                })
                this.resetForm()
                if(this.reportForm.id){
                  this.$router.go(-1);
                }
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      beforeUploadHandle(file) {
        // if (file.type !== 'application/pdf' && file.type !== 'application/msword'
        //   && file.type !== 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
        //   this.$message.error('只支持doc、docx、pdf格式的图片！')
        //   return false
        // }
      },
      successHandle (response, file, fileList) {
        if (response && response.code === 200) {
          this.reportForm.filePath = response.data.url
          this.reportForm.fileSize = response.data.fileSize
        } else {
           this.reportForm.url = ''
           this.reportForm.fileSize =  ''
          this.$message.error(response.msg)
        }
      }
    }
  }
</script>

<style lang='scss' type='text/css'>
  .line-walker {
    margin: 20px 10px 16px;
    height: 1px;
  }

  .matter {
    padding: 0px 120px 20px 20px;
    text-align: left;
    font-size: 12px;
    line-height: 24px;
    color: #969494;

  span {
    color: #fc0000;
  }

  }
</style>
